/**
 * Ref https://blog.csdn.net/chase_hung/article/details/82284788
 */

// Qt
#include <QLabel>
#include <QPushButton>
#include <QQuickItem>
#include <QQuickWidget>

// my
#include "MyWindow.h"

MyWindow::MyWindow(QWidget* parent)
    : QWidget(parent)
    , m_pQuickWidget(nullptr)
    , m_pQuickItem(nullptr)
    , m_pBtnResize(nullptr)
    , m_pBtnLoadQml(nullptr)
    , m_zoomIn(false) {

    // this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->resize(1024, 768);

    // add widget
    addPushButton();
}
//---------------------------------
MyWindow::~MyWindow() {
}
//---------------------------------
void MyWindow::addPushButton() {

    // load qml
    int x = 100;

    m_pBtnResize = new QPushButton(this);
    m_pBtnResize->resize(100, 40);
    m_pBtnResize->move(x, 0);
    m_pBtnResize->setText("resize");

    // Load QML
    m_pBtnLoadQml = new QPushButton(this);
    m_pBtnLoadQml->resize(100, 40);
    m_pBtnLoadQml->move(x + m_pBtnResize->width() + 50, 0);
    m_pBtnLoadQml->setText("Load QML");

    // connect to addQuickWidget
    connect(m_pBtnLoadQml, SIGNAL(clicked()), this, SLOT(addQuickWidget()));
}
//---------------------------------
void MyWindow::addQuickWidget() {
    m_pQuickWidget = new QQuickWidget(this);

    m_pQuickWidget->move(50, 150);
    m_pQuickWidget->setSource(QUrl("qrc:/MyWidget.qml"));
    m_pQuickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    m_pQuickWidget->show();

    m_pQuickItem = m_pQuickWidget->rootObject();
    m_pQuickItem->setProperty("displayWidth", 400);
    m_pQuickItem->setProperty("displayHeight", 300);

    connect(m_pBtnResize, &QPushButton::clicked, [ = ] {
        if (m_zoomIn) {
            m_zoomIn = false;
            m_pQuickItem->setProperty("displayWidth", 800 * 0.8);
            m_pQuickItem->setProperty("displayHeight", 600 * 0.8);
        }
        else {
            m_zoomIn = true;
            m_pQuickItem->setProperty("displayWidth", 800 * 0.4);
            m_pQuickItem->setProperty("displayHeight", 600 * 0.4);
        }
    });
}
